package com.wnnight.wys.web.room;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.wnnight.wys.serviceImpl.room.RoomPageBean;
import com.wnnight.wys.serviceImpl.room.RoomServiceImpl;
import com.wnnight.wys.vo.ResultVo;
import com.wnnight.wys.vo.Room.RoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: Room
 * @Description: //TODO
 * @author: CD
 * @date: 2022/8/29-20:02
 * @version: v1.0
 */


@RestController
@RequestMapping("/api/v1/room")
@Slf4j
public class Room {

    @Autowired
    private RoomServiceImpl roomServiceImpl;


    @PostMapping("/getCount")
    public ResultVo<?> getCount() {
        int i = roomServiceImpl.selectRoomCountzc();
        int i1 = roomServiceImpl.selectRoomCountcz();
        int i2 = roomServiceImpl.selectRoomCountkx();
        ArrayList<Integer> list = new ArrayList<>();
        list.add(i);
        list.add(i1);
        list.add(i2);
        return new ResultVo<>(list);
    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("/listone")
    public ResultVo<?> listone(long id) {
        com.wnnight.wys.entity.room.Room room = roomServiceImpl.selectRoomById1(id);
        return new ResultVo<>(room);
    }

    @PreAuthorize("hasAuthority('rm:update')")
    @PostMapping("/updateRoom")
    public ResultVo<?> updateRoom(@RequestBody com.wnnight.wys.entity.room.Room room) {
        log.debug("修改房间信息");
        log.debug("+++++++++++:{}", room);
        roomServiceImpl.updateRoom11(room);
        return ResultVo.SUCCESS;
    }

    @PreAuthorize("hasAuthority('rm:delete')")
    @PostMapping("/delteLandID")
    public ResultVo<?> delteLandID(long id) {
        roomServiceImpl.deleteLandId(id);
        return ResultVo.SUCCESS;
    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("alllist")
    public ResultVo<?> allist() {
        return new ResultVo<>(roomServiceImpl.list());
    }

    @PreAuthorize("hasAuthority('rm:add')")
    @PostMapping("/importExcel")
    public Boolean importExcel(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<com.wnnight.wys.entity.room.Room> rooms = reader.readAll(com.wnnight.wys.entity.room.Room.class);
        //将excel导入的数据保存到数据库
        roomServiceImpl.saveBatch(rooms);
        return true;
    }

    @PreAuthorize("hasAuthority('rm:select')")
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        List<com.wnnight.wys.entity.room.Room> list = roomServiceImpl.list();
        ExcelWriter writer = ExcelUtil.getWriter(true);//在内存操作，写出到浏览器，从浏览器下载
        //自定义标题名
        writer.addHeaderAlias("id", "id");
        writer.addHeaderAlias("buildingNum", "楼号");
        writer.addHeaderAlias("unit", "单元");
        writer.addHeaderAlias("floor", "楼层");
        writer.addHeaderAlias("roomNum", "房间号");
        writer.addHeaderAlias("peopleNum", "人数");
        writer.addHeaderAlias("status", "状态");
        writer.addHeaderAlias("landlordId", "房东id");
        //一次性写出list内的对象到excel，使用默认格式，强制输出标题
        writer.write(list, true);
        //设置浏览器响应格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("房屋信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        writer.flush(outputStream, true);
        //关闭流
        outputStream.close();
        writer.close();
    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("/selectRoomBYAll")
    public ResultVo<?> selectRoomBYAll(Integer buildingId, Integer unitId) {
        log.debug("查询所有房间");
        log.debug("buildingId:" + buildingId);
        log.debug("unitId:" + unitId);
        return new ResultVo<>(roomServiceImpl.getfoolAllByunbu(buildingId, unitId));

    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("/selectRoomBYCondtion")
    public ResultVo<?> selectRoomBYCondtion(Integer buildingId, Integer unitId) {
        List<com.wnnight.wys.entity.room.Room> rooms = roomServiceImpl.getfoolByunbu(buildingId, unitId);
        log.debug("rooms:{}", rooms);
        return new ResultVo<>(rooms);
    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("/selectBuilding")
    public List<com.wnnight.wys.entity.room.Room> selectBuilding() {
        return roomServiceImpl.selectBuilding();
    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("/foundlist")
    public RoomPageBean.PageBean<com.wnnight.wys.entity.room.Room> foundlist(@RequestBody RoomVo roomVo) {
        log.info("roomVo={},,,,,,,,,,,,,,,", roomVo.getRoom());
        log.info("roomVo={},,,,,,,,,,,,,,,", roomVo.getPageBean());
        RoomPageBean.PageBean<com.wnnight.wys.entity.room.Room> pageallByCondtion = roomServiceImpl.getPageallByCondtion(roomVo.getRoom(), roomVo.getPageBean().getCurrPage(), roomVo.getPageBean().getCurrPageSize());
        return pageallByCondtion;
    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("/list")
    public ResultVo<?> list(Integer page, Integer size) {
        log.debug("page: {}, size: {}", page, size);
        RoomPageBean.PageBean<com.wnnight.wys.entity.room.Room> roomPageBean = roomServiceImpl.selectlistRoom(page == null ? 1 : page, size == null ? 10 : size);

        return new ResultVo<>(roomPageBean);
    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("/list1")
    public ResultVo<?> list1() {
        List<com.wnnight.wys.entity.room.Room> list = roomServiceImpl.list();
        return new ResultVo<>(list);
    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("/getRoomByBuildingId")
    public ResultVo<?> getRoomByBuildingId(Integer buildingId) {
        log.debug("获取小区楼房");
        List<com.wnnight.wys.entity.room.Room> roomList = roomServiceImpl.getRoomByBuildingId(buildingId);
        return new ResultVo<>(roomList);

    }

    @PreAuthorize("hasAuthority('rm:select')")
    @PostMapping("/getRoomByUnitId")
    public ResultVo<?> getRoomByUnitId(@RequestBody Integer unitId) {
        log.debug("获取小区单元");
        List<com.wnnight.wys.entity.room.Room> roomList = roomServiceImpl.getRoomByUnitId(unitId);
        return new ResultVo<>(roomList);
    }


}
